@extends('backstage.layout.master')
@section('title', '主题列表')
@section('style')
    <style>
        a{
            cursor: pointer;
        }
    </style>
@stop
@section('content')
    <div class="breadcrumbs" id="breadcrumbs">
        <script type="text/javascript">
            try {
                ace.settings.check('breadcrumbs', 'fixed')
            } catch (e) {
            }
        </script>

        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
                <a href="backstage/home">主页</a>
            </li>
            <li>
                主题列表
            </li>
        </ul><!-- .breadcrumb -->
    </div>

    <div class="page-content">
        <div class="row">
            <div class="col-xs-12">
                <!-- PAGE CONTENT BEGINS -->
                <div class="page-content">
                    <div class="page-header">
                        <h1 style="display: inline-block;">
                            主题列表
                        </h1>
                        <a href="backstage/types/add"><button class="btn btn-danger add-btn-danger" style="float: right;">添加</button></a>
                    </div><!-- /.page-header -->

                    <div class="row">
                        <div class="col-xs-12">
                            <!-- PAGE CONTENT BEGINS -->
                                <div class="table-responsive">
                                    <table id="sample-table-2" class="table table-striped table-bordered table-hover">
                                        <thead>
                                        <tr>
                                            <th>降序排列</th>
                                            <th>主题编号</th>
                                            <th>主题名称</th>
                                            <th>主题预览图</th>
                                            <th>主题关联模块</th>
                                            <th>显示状态</th>
                                            <th>功能性按钮</th>
                                            <th>
                                                <i class="icon-time bigger-110 hidden-480"></i>
                                                创建时间
                                            </th>
                                            <th>操作</th>
                                        </tr>
                                        </thead>

                                        <tbody>
                                        @foreach ($types as $type)
                                            <tr>
                                                <td style="width:68px;">
                                                    <input type="text" name="sort" value="{{$type->sort}}" class="sort"
                                                           style="width:50px;" data="{{$type->sort}}"
                                                           data-module-id="{{$type->type_id}}"
                                                           onblur="updateModule(this,2);">
                                                </td>
                                                <td>{{$type->type_id}}</td>
                                                <td>
                                                    {{$type->type_name}}
                                                </td>
                                                <td>
                                                    <img style="width:100px;" src="{{$type->type_image}}">
                                                </td>
                                                <td>
                                                    @if($type->app_id == 3)
                                                        产品模块
                                                    @endif
                                                </td>
                                                <td>
                                                    @if($type->is_show == 0)
                                                        <a class="red" data="{{$type->is_show}}" data-module-id="{{$type->type_id}}" onclick="updateModule(this,1);">隐藏</a>
                                                    @else
                                                        <a class="blue" data="{{$type->is_show}}" data-module-id="{{$type->type_id}}" onclick="updateModule(this,1);">显示</a>
                                                    @endif
                                                </td>
                                                <td>
                                                    @if($type->is_features == 0)
                                                        主题
                                                    @else
                                                        功能按钮
                                                    @endif
                                                </td>
                                                <td class="hidden-480">{{date('Y-m-d H:i:s',$type->updated_at)}}</td>
                                                <td>
                                                    <a href="backstage/types/update/{{$type->type_id}}">编辑</a>
                                                    <a class="red" href="backstage/types/delete/{{$type->type_id}}" onclick="return confirm('确认删除吗？');">删除</a>
                                                    <a class="green" href="backstage/types/product-list/{{$type->type_id}}">产品列表</a>
                                                    <a class="green" href="backstage/types/add-product/{{$type->type_id}}">添加产品</a>
                                                </td>
                                            </tr>
                                        @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            <div class="row">
                                <div>
                                    <div class="dataTables_paginate paging_bootstrap">
                                        <?php echo $types->render(); ?>
                                    </div>
                                </div>
                            </div>
                            <!-- PAGE CONTENT ENDS -->
                        </div><!-- /.col -->
                    </div><!-- /.row -->
                </div><!-- /.page-content -->
            </div>
        </div>
    </div>
@stop
@section('script')
    <script type="text/javascript">
        var flag = 1;
        $(function(){
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
        });

        function updateModule(th, type) {
            if (type == 1) {
                //修改显示
                var is_show = $(th).attr("data") == 1 ? 0 : 1;
                show(th, is_show, $(th).attr("data-module-id"));
            } else if (type == 2) {
                //修改排序
                var newsort = $(th).val();
                var oldsort = $(th).attr('data');
                if (newsort == oldsort) {
                    return false;
                }
                changeSort(th, newsort, $(th).attr("data-module-id"));
            } else {
                //删除
                if (confirm('确认删除吗？')) {
                    deleteModule(th, $(th).attr("data-module-id"));
                }
            }
        }
        function show(th, is_show, type_id) {
            if (flag == 1) {
                flag = 0;
                $.ajax({
                    type: "POST",
                    url: "backstage/types/show",
                    data: {"is_show": is_show, "type_id": type_id},
                    success: function (msg) {
                        flag = 1;
                        if (msg.state == "M00000") {
                            $(th).attr("data", msg.result.is_show);
                            showMessage("", msg.message);
                            window.location.reload();
                        } else {
                            showMessage(msg.message, "");
                        }
                    },
                    error: function () {
                        flag = 1;
                        showMessage("修改失败!", "");
                    }
                });
            }
        }
        function changeSort(th, sort, type_id) {
            if (flag == 1) {
                flag = 0;
                $.ajax({
                    type: "POST",
                    url: "backstage/types/update-sort",
                    data: {"sort": sort, "type_id": type_id},
                    success: function (msg) {
                        flag = 1;
                        if (msg.state == "M00000") {
                            $(th).attr("data", msg.result.sort);
                            showMessage("", msg.message);
                        } else {
                            showMessage(msg.message, "");
                        }
                    },
                    error: function () {
                        flag = 1;
                        showMessage("修改失败!", "");
                    }
                });
            }
        }
    </script>
@stop